package com.rocpenghua.excel.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Workbook;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DataDealUtil {
    public static Workbook dataToInputStream(String tableName,
                                             String tableUnit,
                                             String tableData,
                                             String tableTitle,
                                             int cellWidth,
                                             int cellHeight,
                                             String type) throws Exception {
        String rootId = "title";
        String pId = "unit";
        if (tableUnit == null || "".equals(tableUnit)) {
            pId = "title";
        }
        JSONArray titleArray = JSON.parseArray(tableTitle);//总的表头
        List<TitleEntity> titleEntityList = new ArrayList<>();
        List<String> valueTitles = new ArrayList<String>();
        TitleEntity t0 = new TitleEntity(rootId, null, tableName, null);
        titleEntityList.add(t0);//一级表名称
        if ("unit".equals(pId)) {
            TitleEntity t1 = new TitleEntity(pId, rootId, tableUnit, null);
            titleEntityList.add(t1);//二级表数据
        }
        for (int i = 0; i < titleArray.size(); i++) {
            JSONObject jsonObject = (JSONObject) titleArray.get(i);//每一列的表头
            if (jsonObject.containsKey("code")) {//直接展示数据列
                TitleEntity t = new TitleEntity("" + i, pId, jsonObject.get("name").toString(), jsonObject.get("code").toString());
                titleEntityList.add(t);
                valueTitles.add(jsonObject.get("code").toString());
            } else {
                TitleEntity t = new TitleEntity("" + i, pId, jsonObject.get("name").toString(), null);
                titleEntityList.add(t);
                if (jsonObject.containsKey("children")) {
                    JSONArray titleArray1 = (JSONArray) jsonObject.get("children");
                    //先加完二级再添加三级
                    for (int j = 0; j < titleArray1.size(); j++) {
                        JSONObject subObject = (JSONObject) titleArray1.get(j);
                        if (subObject.containsKey("code")) {
                            t = new TitleEntity(i + "_" + j, "" + i, subObject.get("name").toString(), subObject.get("code").toString());
                            titleEntityList.add(t);
                            valueTitles.add(subObject.get("code").toString());
                        } else {
                            t = new TitleEntity(i + "_" + j, "" + i, subObject.get("name").toString(), null);
                            titleEntityList.add(t);
                        }
                    }
                    //添加三级
                    for (int j = 0; j < titleArray1.size(); j++) {
                        JSONObject subObject = (JSONObject) titleArray1.get(j);
                        if (subObject.containsKey("children")) {
                            JSONArray titleArray2 = (JSONArray) subObject.get("children");
                            for (int k = 0; k < titleArray2.size(); k++) {
                                JSONObject subObject2 = (JSONObject) titleArray2.get(k);
                                if (subObject2.containsKey("code")) {
                                    t = new TitleEntity(i + "_" + j + "_" + k, i + "_" + j, subObject2.get("name").toString(), subObject2.get("code").toString());
                                    titleEntityList.add(t);
                                    valueTitles.add(subObject2.get("code").toString());
                                } else {
                                    t = new TitleEntity(i + "_" + j + "_" + k, i + "_" + j, subObject2.get("name").toString(), null);
                                    titleEntityList.add(t);
                                }
                            }
                        }
                    }
                }
            }
        }
        List<Map<String, Object>> rowList;
        if (StringUtils.isNotBlank(tableData)) {
            JSONArray dataArray = JSON.parseArray(tableData);
            rowList = new ArrayList<Map<String, Object>>(dataArray.size());
            for (int i = 0; i < dataArray.size(); i++) {
                JSONObject obj = (JSONObject) dataArray.get(i);
                Map<String, Object> map = new HashMap<String, Object>();
                for (int j = 0; j < valueTitles.size(); j++) {
                    map.put(valueTitles.get(j), obj.get(valueTitles.get(j)));
                }
                rowList.add(map);
            }
        } else {
            rowList = new ArrayList<>();
        }
        ExcelTool excelTool;
        if("xlsx".equals(type)){
            excelTool = new XssfExcelTool(tableName, cellWidth, cellHeight);
        }else{
            excelTool = new HssfExcelTool(tableName, cellWidth, cellHeight);
        }
        List<Column> titleData = excelTool.columnTransformer(titleEntityList, "id", "pid", "content", "fileName", "title");
        return excelTool.exportWorkbook(titleData, rowList, true);
    }

}
